/**
 * @Title: CrShopOrderService.java
 * @Package com.lemon.service.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月26日
 * @version V1.0
 */
package com.lemon.service.cityRun;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gexin.rp.sdk.base.IPushResult;
import com.github.pagehelper.PageHelper;
import com.lemon.conf.MyException;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.conf.redis.RedisUtil;
import com.lemon.mapper.cityRun.CrShopActivityOutDiscountMapper;
import com.lemon.mapper.cityRun.CrShopActivityOutMapper;
import com.lemon.mapper.cityRun.CrShopGoodsMapper;
import com.lemon.mapper.cityRun.CrShopGoodsSpecMapper;
import com.lemon.mapper.cityRun.CrShopOrderCommentMapper;
import com.lemon.mapper.cityRun.CrShopOrderGoodsMapper;
import com.lemon.mapper.cityRun.CrShopOrdersMapper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.mapper.cityRun.CrUserTransactionRecordMapper;
import com.lemon.model.cityRun.CrShopGoods;
import com.lemon.model.cityRun.CrShopGoodsSpec;
import com.lemon.model.cityRun.CrShopOrderComment;
import com.lemon.model.cityRun.CrShopOrderGoods;
import com.lemon.model.cityRun.CrShopOrders;
import com.lemon.model.cityRun.CrShopSet;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrUserTransactionRecord;
import com.lemon.utils.CityRunUtils;
import com.lemon.utils.StringUtil;
import com.lemon.utils.appPush.AppPush;

/**
 * <p>
 * Title: CrShopOrderService
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月26日
 * @版本 V 1.0
 */
@Service
public class CrShopOrderServiceImpl implements CrShopOrderService {
	private static final Logger logger = LoggerFactory.getLogger(CrShopOrderServiceImpl.class);
	@Resource
	private CrShopOrdersMapper oMapper;
	@Resource
	private CrShopOrderGoodsMapper gMapper;
	@Resource
	private CrShopOrderCommentMapper cMapper;
	@Resource
	private CrShopGoodsMapper goMapper;

	@Resource
	private CrShopGoodsSpecMapper specMapper;

	@Resource
	private CrShopActivityOutDiscountMapper disMapper;
	@Resource
	private CrShopActivityOutMapper actMapper;

	@Resource
	private ShopPrintService pService;
	@Resource
	private CrShopUserMapper uMapper;

	@Resource
	private CrUserTransactionRecordMapper transMapper;
	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月26日
	 */
	@Override
	public List<CrShopOrders> selectListBySelective(CrShopOrders order) throws Exception {
		PageHelper.startPage(order.getPageNum() , order.getPageSize());
		return oMapper.selectListBySelective(order);
	}

	/**
	 * <p>
	 * Title: selectOrderGoodsListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param ogo
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月26日
	 */
	@Override
	public List<CrShopOrderGoods> selectOrderGoodsListBySelective(CrShopOrderGoods ogo) throws Exception {

		return gMapper.selectListBySelective(ogo);
	}

	/**
	 * <p>
	 * Title: selectOrderProfitToday
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月26日
	 */
	@Override
	public List<Map<String , Object>> selectOrderProfitToday(CrShopOrders order) throws Exception {

		return oMapper.selectOrderProfitToday(order);
	}

	/**
	 * <p>
	 * Title: updateCommentBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月1日
	 */
	@Override
	public int updateCommentBySelective(CrShopOrderComment comment) throws Exception {

		return cMapper.updateByPrimaryKeySelective(comment);
	}

	/**
	 * <p>
	 * Title: selectCommentsListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月1日
	 */
	@Override
	public List<CrShopOrderComment> selectCommentsListBySelective(CrShopOrderComment comment) throws Exception {
		PageHelper.startPage(comment.getPageNum() , comment.getPageSize());
		List<CrShopOrderComment> list = cMapper.selectListBySelective(comment);
		return list;
	}

	/**
	 * <p>
	 * Title: getOrderProfitList
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param orders
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月5日
	 */
	//	@Cacheable(value = "city:porfit#3600#600" , keyGenerator = "keyGenerator" , unless = "#result == null")
	@Override
	public List<Map<String , Object>> getOrderProfitList(CrShopOrders orders) throws Exception {
		PageHelper.startPage(orders.getPageNum() , orders.getPageSize());
		return oMapper.selectOrderProfitOnDate(orders);
	}

	/**
	 * <p>
	 * Title: getShopsCommentList
	 * </p>
	 * <p>
	 * Description:(一句话阐述): wx查询商家评论列表
	 * </p>
	 * 
	 * @param shops
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月9日
	 */
	@Override
	public List<CrShopOrderComment> getShopsCommentList(CrShopOrderComment shops) throws Exception {
		PageHelper.startPage(shops.getPageNum() , shops.getPageSize());
		return cMapper.getShopsCommentList(shops);
	}

	/**
	 * <p>
	 * Title: getShopTodayFirgit
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param orders
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月10日
	 */
	@Override
	public Map<String , Object> getShopTodayFirgit(CrShopOrders orders) throws Exception {
		return oMapper.getShopTodayFirgit(orders);
	}

	/**
	 * <p>
	 * Title: createOrdersInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int createOrdersInfo(CrShopOrders order) throws Exception {

		String ids = order.getGoodsIds();
		String[] split = ids.split(",");  //菜品列表
		CrShopOrderGoods csog = null;
		CrShopGoods goods = null;

		CrShopGoodsSpec goodsSpec = null;

		int prr = 0;

		//先去修改里面菜品的数量, 修改成功说明可以购买 , 如果修改失败说明商品售罄或者商品不存在  创建订单失败返回信息
		for (int i = 0; i < split.length; i++) {
			String id_num = split[i];
			String[] goodsinfo = id_num.split("-");
			String specId = goodsinfo[4];
			prr += (Integer.parseInt(goodsinfo[1]) * Integer.parseInt(goodsinfo[2]));
			int stock = Integer.parseInt(goodsinfo[1]);
			int row = 0;
			if (!"0".equals(specId)) {
				goodsSpec = new CrShopGoodsSpec();
				goodsSpec.setId(specId);

				CrShopGoodsSpec goodsSpec2 = new CrShopGoodsSpec();
				goodsSpec2.setGoodsId(goodsinfo[0]);
				List<CrShopGoodsSpec> bySelective = specMapper.selectListBySelective(goodsSpec2);
				if (bySelective.size() == 1) {
					//修改商品的库存信息
					goods = new CrShopGoods();
					goods.setId(goodsinfo[0]);
					goods.setGoodsStock(-stock);
					row = goMapper.updateStockNumById(goods);
					if (row == 0) {
						throw new Exception("商品[" + goodsinfo[3] + "]库存不足,创建订单失败");
					}
				}
				goodsSpec.setGoodsStock(-stock);
				row = specMapper.updateStockNumBySpecId(goodsSpec);
				if (row == 0) {
					new Exception("商品[" + goodsinfo[3] + "]库存不足,创建订单失败");
				}
				//如果选择了规格就去修改规格的库存信息
			} else {
				//修改商品的库存信息
				goods = new CrShopGoods();
				goods.setId(goodsinfo[0]);
				goods.setGoodsStock(-stock);
				row = goMapper.updateStockNumById(goods);
				if (row == 0) {
					throw new Exception("商品[" + goodsinfo[3] + "]库存不足,创建订单失败");
				}
			}
		}
		//获取店铺的活动列表
		/*
		 * CrShopActivityOut act = new CrShopActivityOut();
		 * act.setShopId(order.getShopId());
		 * act.setStatus(1);
		 * List<CrShopActivityOut> actList = actMapper.selectListBySelective(act);
		 * //activityIds 查询用户参加的活动
		 * int price = 0;
		 * //计算菜品价格
		 * for (int i = 0; i < split.length; i++) {
		 * String id_num = split[i];
		 * String[] goodsinfo = id_num.split("-");
		 * String specId = goodsinfo[4];
		 * int stock = Integer.parseInt(goodsinfo[1]);
		 * String goodsId = goodsinfo[0];
		 * if (!"0".equals(specId)) {
		 * goodsSpec = new CrShopGoodsSpec();
		 * goodsSpec.setId(specId);
		 * CrShopGoodsSpec spec = specMapper.selectByPrimaryKey(goodsSpec);
		 * if (order.getType() == 1) {
		 * price += spec.getPriceOut() * stock;
		 * } else {
		 * price += spec.getPriceRoom() * stock;
		 * }
		 * price += spec.getGoodsPackAmount();
		 * } else {
		 * //
		 * CrShopGoods goods2 = goMapper.selectByPrimaryKey(goodsId);
		 * //查询外卖商品是否有折扣信息
		 * if (order.getType() == 1) {
		 * //先查询商家开启了什么优惠活动
		 * boolean g = false;
		 * for (int j = 0; j < actList.size(); j++) {
		 * Integer activityType = actList.get(j).getActivityType();
		 * if (activityType == 3) {
		 * CrShopActivityOutDiscount disc = new CrShopActivityOutDiscount();
		 * disc.setGoodsId(goodsId);
		 * disc.setStatus(1);
		 * CrShopActivityOutDiscount discount = disMapper.selectBySelective(disc);
		 * if (discount == null) {
		 * continue;
		 * }
		 * //判断折扣商品限购
		 * if (stock > discount.getForNum()) {
		 * price += discount.getForNum() * discount.getGoodsPriceDis(); //计算折扣价
		 * price += (stock - discount.getForNum()) * goods2.getPriceOut(); //计算折扣价剩下数量的原价
		 * g = true;
		 * }
		 * }
		 * }
		 * //如果没有参加折扣活动则按原价计算
		 * if (!g) {
		 * price += goods2.getPriceOut() * stock;
		 * }
		 * } else {
		 * price += goods2.getPriceRoom() * stock;
		 * }
		 * price += goods2.getGoodsPackAmount();
		 * }
		 * }
		 */
		String activityIds = order.getActivityIds();
		int leftPrice = order.getPayPrice();
		if (StringUtil.isNotBlank(activityIds)) {
			String[] split2 = activityIds.split(",");
			for (int j = 0; j < split2.length; j++) {
				String ooo = split2[j];
				String[] oot = ooo.split("-");
				if (oot.length <= 0) {
					continue;
				}
				if ("3".equals(oot[0])) {
					leftPrice += Integer.parseInt(oot[1]);
					continue;
				} else {
					prr = prr - Integer.parseInt(oot[1]);
				}
			}
		}
		prr += order.getPackPrice() + order.getSendPrice();
		System.out.println(prr); //wode 
		System.out.println(leftPrice); // chuande 
		//后期加上价格判断

		//		  if (prr != leftPrice) {
		//		  throw new Exception("订单价格出现异常!");
		//		  }
		if (0 > leftPrice) {
			throw new Exception("订单价格出现异常!");
		}

		StringBuffer tt = new StringBuffer();
		//付款之后判断是否自动接单
		CrShopSet set = RedisUtil.get("cityRun:set:set_" + order.getShopId() , CrShopSet.class);
		if (set != null) {
			if (order.getType() == 1) {

				Integer orderOut = set.getGetOrderOut();
				if (orderOut != null && orderOut == 2) {
						//自动接单 状态变成待送达状态
						order.setStatus(3);
					} else {
						//手动接单 状态变成待接单状态
						order.setStatus(2);
					}
				tt.append(set.getGetOrderOut());
			} else {
				//堂食
				Integer orderOut = set.getGetOrderRoom();
				if (orderOut != null && orderOut == 2) {
					//自动接单 状态变成待送达状态
					order.setStatus(3);
					tt.append(set.getGetOrderRoom());
				} else if (orderOut == 3) {
					//微信支付自动接单, 现金结算手动接单
					//判断是微信支付还是手动支付
					if (order.getPayType() == 1) {
						//微信支付
						//自动接单 状态变成待送达状态
						tt.append(2);
						order.setStatus(3);
					} else {
						//手动接单 状态变成待接单状态

						order.setStatus(2);
					}
				} else {
					//手动接单 状态变成待接单状态
					order.setStatus(2);
				}
				order.setSendPrice(0);
			}

			order.setStartDate(CityRunUtils.getBusineDateString());
		} else {
			order.setStatus(2);
		}
		if (order.getPayType() == 1) {
			order.setStatus(1);
		}
		//判断是否是堂食

		if (order.getType() == 2) {
			order.setUserName(IdTypeHandler.decode(order.getUserName()) + "");
		}

		//先创建订单,  返回订单id
		int insertSelective = oMapper.insertSelective(order);

		//修改账户余额
		//修改用户账户信息
		/*
		 * CrShopUser u = new CrShopUser();
		 * u.setWhere(" and b.id = ?  " , IdTypeHandler.decode(order.getShopId()));
		 * u = uMapper.selectBySelective(u);
		 * CrShopUser uu = new CrShopUser();
		 * uu.setId(u.getId());
		 * uu.setAmount(order.getPayPrice());
		 * int selective = uMapper.updateByPrimaryKeySelective(u);
		 */
		//将订单id存入购买的菜品中

		List<CrShopOrderGoods> list2 = new ArrayList<CrShopOrderGoods>();
		for (int i = 0; i < split.length; i++) {
			csog = new CrShopOrderGoods();
			String id_num = split[i];
			String[] goodsinfo = id_num.split("-");
			csog.setGoodsId(goodsinfo[0]);
			csog.setGoodsNum(Integer.parseInt(goodsinfo[1]));
			csog.setGoodsPrice(Integer.parseInt(goodsinfo[2]));
			csog.setGoodsTitle(goodsinfo[3]);
			csog.setOrderId(IdTypeHandler.encode(Long.parseLong(order.getId())));
			csog.setStatus(1);
			String spe = goodsinfo[4];
			if ("0".equals(spe)) {
				csog.setGoodsSpecId("74");
			} else {
				csog.setGoodsSpecId(spe);
			}
			list2.add(csog);
		}
		//批量插入订单商品
		gMapper.insertBatch(list2);
		//判断是否现金结算 ,微信支付的走微信回调接口
		if (order.getPayType() == 2) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(5 * 1000);
						String orderNo = "";
						int row = 0;
						CrShopOrders ord = order;

						logger.info("另起线程开始打印任务");
						CrShopOrders or = new CrShopOrders();
						or.setOrderNo(ord.getOrderNo());
						CrShopOrders order = oMapper.selectBySelective(or);
						if (order == null) {
							return;
						}
						orderNo = ord.getOrderNo();
						try {
							logger.info("开始发送短信到客户端");
							CrShopUser u = new CrShopUser();
							u.setWhere(" and b.id = ? " , IdTypeHandler.decode(order.getShopId()));
							CrShopUser user = uMapper.selectBySelective(u);
							String cid = user.getCid();
							AppPush push = new AppPush();
							push.setCid(cid);
							push.setContent(orderNo);
							push.setTitle("新订单通知");
							//判断是否自动接单
							StringBuffer t = tt;
							logger.info("是否自动接单{}" , t.toString());

							if ("2".equals(t.toString())) {
								push.setType(12);
							} else {
								push.setType(1);
							}
							push.setText("您有一个新订单,点击查看");
							push.setOrderType(ord.getType());
							IPushResult ret = AppPush.pushMsgToSingle(push);
							logger.info(ret.getResponse().toString());
						} catch (Exception e) {
							MyException.writeErr(e , "发送订单短信到app失败");
						}

						CrShopSet set = RedisUtil.get("cityRun:set:set_" + order.getShopId() , CrShopSet.class);
						if (set != null) {
							if (order.getType() == 1) {
								Integer orderOut = set.getGetOrderOut();
								if (orderOut != null && orderOut == 2) {
									//自动接单 状态变成待送达状态
									order.setStatus(3);
								} else {
									//手动接单 状态变成待接单状态
									order.setStatus(2);
								}
							} else {
								//堂食
								Integer orderOut = set.getGetOrderRoom();
								if (orderOut != null && orderOut == 2) {
									//自动接单 状态变成待送达状态
									order.setStatus(3);
								} else if (orderOut == 3) {
									//微信支付自动接单, 现金结算手动接单
									//判断是微信支付还是手动支付
									if (order.getPayType() == 1) {
										//微信支付
										//自动接单 状态变成待送达状态
										order.setStatus(3);
									} else {
										//手动接单 状态变成待接单状态
										order.setStatus(2);
									}
								} else {
									//手动接单 状态变成待接单状态
									order.setStatus(2);
								}
							}
							row = oMapper.updateByPrimaryKeySelective(order);
						}
						if (row > 0) {
							//判断店铺设置是否自动打印()
							if (order.getStatus() == 3) {
								logger.info("接单状态,需要打印");
								pService.printerInfo(order.getShopId() , order.getId());
							}
							//							Integer printAuto = set.getPrintAuto();
							//							if (printAuto == 1) {
								//自动打印小票  调用打印机 , 内部已经处理打印机列表以及打印机类型 然后自动打印
								//								pService.printerInfo(order.getShopId() , order.getId());
							//							}
						} else {
						}

					} catch (Exception e) {
						MyException.writeErr(e , "");
					}
				}
			}).start();
		}

		return insertSelective == 0 ? 0 : Integer.parseInt(order.getId());
	}

	/**
	 * <p>
	 * Title: selectCountBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param or
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月12日
	 */
	@Override
	public int selectCountBySelective(CrShopOrders or) throws Exception {

		return oMapper.selectCountBySelective(or);
	}

	/**
	 * <p>
	 * Title: updateOrderInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月14日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int updateOrderInfo(CrShopOrders order) throws Exception {

		CrShopOrders orders = oMapper.selectByPrimaryKey(order.getId());

		if (order.getStatus() != null && order.getStatus() == 3 && orders.getType() == 2) {
			if (orders.getPayType() == 1) {
				order.setStatus(31);
			}
		}
		//取消或者拒单  将商品数量返回
		if (order.getStatus() != null && (order.getStatus() == 51 || order.getStatus() == 52)) {
			updateCelGoodsNum(order);
		}
		int i = oMapper.updateByPrimaryKeySelective(order);
		orders = oMapper.selectByPrimaryKey(order.getId());
		if (order.getStatus() != null && order.getStatus() == 4 && orders.getPayType() == 1) {
			CrShopUser u = new CrShopUser();
			u.setWhere(" and b.id = ?  " , IdTypeHandler.decode(orders.getShopId()));
			u = uMapper.selectBySelective(u);
			CrShopUser uu = new CrShopUser();
			uu.setId(u.getId());
			uu.setAmount(orders.getPayPrice());
			int selective = uMapper.updateUserAmount(uu);
			
			CrUserTransactionRecord tran = new CrUserTransactionRecord();
			tran.setSerialNumber(orders.getOrderNo());
			CrUserTransactionRecord record = transMapper.selectBySelective(tran);
			if (record != null) {
				tran.setId(record.getId());
				tran.setStatus(1);
				transMapper.updateByPrimaryKeySelective(tran);
			}

			/*
			 * CrUserTransactionRecord tran = new CrUserTransactionRecord();
			 * tran.setShopUserId(u.getId());
			 * tran.setShopAccount(u.getUserAccount());
			 * tran.setShopTitle(orders.getShopTitle());
			 * tran.setShopId(orders.getShopId());
			 * tran.setSerialNumber(orders.getOrderNo());
			 * tran.setAmount(orders.getPayPrice());
			 * tran.setSerialNumber(StringUtil.createSerialNoCityRun());
			 * tran.setStatus(1);
			 * tran.setType(2);
			 * tran.setTranType(1);
			 * transMapper.insertSelective(tran);
			 */
			
		}
		return i;
	}

	/**
	 * <p>
	 * Title: payWeChatNotifyUrl
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @author Ran_cc
	 * @date 2018年11月24日
	 */
	@Override
	public void payWeChatNotifyUrl(HttpServletRequest request , HttpServletResponse response) {
	}

	/**
	 * <p>
	 * Title: selectByOrderId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年12月1日
	 */
	@Override
	public CrShopOrders selectByOrderId(String id) throws Exception {

		return oMapper.selectByPrimaryKey(id);
	}

	/**
	 * <p>
	 * Title: celOrdersById
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param id
	 * @author Ran_cc
	 * @date 2018年12月1日
	 */
	@Override
	public void celOrdersById(String id) throws Exception {

	}

	/**
	 * <p>
	 * Title: insertCommentBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年12月2日
	 */
	@Override
	public int insertCommentBySelective(CrShopOrderComment comment) throws Exception {

		return cMapper.insertSelective(comment);
	}

	/**
	 * <p>
	 * Title: selectCommentBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年12月2日
	 */
	@Override
	public CrShopOrderComment selectCommentBySelective(CrShopOrderComment comment) throws Exception {

		return cMapper.selectBySelective(comment);
	}

	public static void main(String[] args) {
		String cn = "大桌4号";
		System.out.println(cnToUnicode(cn));
		// 字符串 : \u5f00\u59cb\u4efb\u52a1 ，由于 \ 在java里是转义字符，要写出下面这种形式
		String unicode = "\\u4f60";
		System.out.println(unicodeToCn(unicode));
	}

	private static String unicodeToCn(String unicode) {
		/** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格 */
		String[] strs = unicode.split("\\\\u");
		String returnStr = "";
		// 由于unicode字符串以 \ u 开头，因此分割出的第一个字符是""。
		for (int i = 1; i < strs.length; i++) {
			returnStr += (char) Integer.valueOf(strs[i] , 16).intValue();
		}
		return returnStr;
	}

	private static String cnToUnicode(String cn) {
		char[] chars = cn.toCharArray();
		String returnStr = "";
		for (int i = 0; i < chars.length; i++) {
			returnStr += "\\u" + Integer.toString(chars[i] , 16);
		}
		return returnStr;
	}

	/**
	 * <p>
	 * Title: updateCelGoodsNum
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order2
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2019年1月5日
	 */
	@Override
	public void updateCelGoodsNum(CrShopOrders order) throws Exception {

		//查询出来订单中的商品信息
		CrShopOrderGoods csg = new CrShopOrderGoods();
		csg.setOrderId(order.getId());
		List<CrShopOrderGoods> list = gMapper.selectListBySelective(csg);
		for (int i = 0; i < list.size(); i++) {
			CrShopOrderGoods orderGoods = list.get(i);
			//去修改对应的商品数量
			//判断是否有规格 , 有规格就去修改规格的数量
			CrShopGoodsSpec goodsSpec = new CrShopGoodsSpec();
			goodsSpec.setId(orderGoods.getGoodsSpecId());

			int row = 0;
			/*
			 * CrShopGoodsSpec goodsSpec2 = new CrShopGoodsSpec();
			 * goodsSpec2.setGoodsId(orderGoods.getGoodsId());
			 * List<CrShopGoodsSpec> bySelective = specMapper.selectListBySelective(goodsSpec2);
			 * if (bySelective.size() == 1) {
			 */
				//修改商品的库存信息
				CrShopGoods goods = new CrShopGoods();
				goods.setId(orderGoods.getGoodsId());
				goods.setGoodsStock(orderGoods.getGoodsNum());
				row = goMapper.updateStockNumById(goods);

			//			}
			goodsSpec.setGoodsStock(orderGoods.getGoodsNum());
			row = specMapper.updateStockNumBySpecId(goodsSpec);

		}
	}
}
